<?php

/**
 * @desc Connects to the MySQL database
 * @param null
 * @return null
 */
function connectToDatabase() {
	require 'dbInfo.php';
	$db_server = mysql_connect($db_hostname, $db_username, $db_password);
	if (!$db_server
	)die("Cannot connect to the MySQL database!<br />Please contact your database administrator.");
	mysql_select_db($db_database) or die("Cannot select the database!<br />Please make sure it has been installed properly!");
}

/**
 * @desc Displays a select box for categories
 * @param string $name
 */
function showCategories($name) {

	$result = mysql_query("SELECT name FROM categories");
	if (!$result
	)die("Could not complete the query...");
	$numRows = mysql_num_rows($result);
	echo "<form action='index.php' method='post'>";
	echo "<div align='left'>Category: <select onchange='this.form.submit()' name='categoryChange'></div>";
	if ($name == null) {
		echo "<option SELECTED value=''>ALL</option>";
	} else {
		echo "<option value=''>ALL</option>";
	}
	for ($i = 0; $i < $numRows; $i++) {
		$row = mysql_fetch_assoc($result);
		if ($row[name] == $name) {
			echo "<option SELECTED value='$row[name]'>$row[name]</option>";
		} else {
			echo "<option value='$row[name]'>$row[name]</option>";
		}
	}
	echo "</select>";
	echo "</form>";
}

/**
 * @desc Sorts the list by date added
 * @param null
 * @return null
 */
function filterDate() {

	$categories = $_SESSION['category'];
	$query = "SELECT o.city, o.state, o.name, io.* FROM INTERNSHIP_OPPORTUNITY io, ORGANIZATION o WHERE io.organizationID = o.OrganizationID AND io.categories LIKE '%$categories%' ORDER BY io.dateAdded DESC";

	$result = mysql_query($query);
	if (!$result
	)die("Could not complete query...");
	$numRows = mysql_num_rows($result);
	echo "<table width='100%' cellspacing=0><tr><td width='10%'></td><td><b>Location</b></td><td><b>Position</b></td><td><b>Company</b></td><td width='10%'><b>Positions Available</b></td><td><b>Pay Rate</b></td><td><b>Semester</b></td><td width='30%'><b>Date Added</b></td></tr>";
	echo "<tr><td colspan=8><hr /></td></tr>";
	for ($x = 0; $x < $numRows; $x++) {
		$row = mysql_fetch_assoc($result);
		if ($x % 2 == 0) {
			echo "<tr>";
		} else {
			echo "<tr bgcolor='#FBFBFB'>";
		}
		echo "<td align='center' valign='middle'><br /><form action='index.php' method='post'><input type='hidden' name='viewCompany' value='$row[organizationID]'><input type='submit' value='View' class='submit' name='view' /></form></td>";
		echo "<td>$row[city], " . strtoupper($row[state]) . "</td>";
		echo "<td>$row[title]</td>";
		echo "<td>$row[name]</td>";
		echo "<td>$row[numPositionsAvailable]</td>";
		echo "<td>$row[payRate]</td>";
		echo "<td>$row[semester]</td>";
		$desc = $row[dateAdded];
		/* if (strlen($desc) > 50) {
		 $desc = substr($desc, 0, -(strlen($desc) - 50)) . "...";
		 } */
		echo "<td>$desc</td>";
		echo "</tr>";
	}
	echo "</table>";
}

/**
 * @desc Sorts internships by paid or unpaid
 * @param null
 * @return null
 */
function filterPaid() {

	$categories = $_SESSION['category'];
	$query = "SELECT o.city, o.state, o.name, io.* FROM INTERNSHIP_OPPORTUNITY io, ORGANIZATION o WHERE io.organizationID = o.OrganizationID AND io.categories LIKE '%$categories%' ORDER BY io.payRate DESC";

	$result = mysql_query($query);
	if (!$result
	)die("Could not complete query...");
	$numRows = mysql_num_rows($result);
	echo "<table width='100%' cellspacing=0><tr><td width='10%'></td><td><b>Location</b></td><td><b>Position</b></td><td><b>Company</b></td><td width='10%'><b>Positions Available</b></td><td><b>Pay Rate</b></td><td><b>Semester</b></td><td width='30%'><b>Date Added</b></td></tr>";
	echo "<tr><td colspan=8><hr /></td></tr>";
	for ($x = 0; $x < $numRows; $x++) {
		$row = mysql_fetch_assoc($result);
		if ($x % 2 == 0) {
			echo "<tr>";
		} else {
			echo "<tr bgcolor='#FBFBFB'>";
		}
		echo "<td align='center' valign='middle'><br /><form action='index.php' method='post'><input type='hidden' name='viewCompany' value='$row[organizationID]'><input type='submit' value='View' class='submit' name='view' /></form></td>";
		echo "<td>$row[city], " . strtoupper($row[state]) . "</td>";
		echo "<td>$row[title]</td>";
		echo "<td>$row[name]</td>";
		echo "<td>$row[numPositionsAvailable]</td>";
		echo "<td>$row[payRate]</td>";
		echo "<td>$row[semester]</td>";
		$desc = $row[dateAdded];
		/* if (strlen($desc) > 50) {
		 $desc = substr($desc, 0, -(strlen($desc) - 50)) . "...";
		 } */
		echo "<td>$desc</td>";
		echo "</tr>";
	}
	echo "</table>";
}

/**
 * @desc Views a company when the user click on an appropriate button
 * @param int $ID
 * @return null
 */
function viewCompany($ID) {
	$company = mysql_query("SELECT name FROM organization WHERE organizationID='$ID'");
	$row = mysql_fetch_assoc($company);
	echo "<div align='left'><h1>" . $row[name] . "</h1></div>";
	$query = "SELECT * FROM internship_opportunity WHERE organizationID='$ID' ORDER BY semester";
	$result = mysql_query($query);
	if (!$result
	)die("Could not complete query...");
	$numRows = mysql_num_rows($result);
	echo "<table width='100%' cellspacing=0><tr><td width='10%'></td><td><b>Title</b></td><td width='10%'><b>Positions Available</b></td><td><b>Semester</b></td><td><b>Paid</b></td><td><b>Duration</b></td><td><b>Date Added</b></td></tr>";
	echo "<tr><td colspan=7><hr /></td></tr>";
	for ($x = 0; $x < $numRows; $x++) {
		$row = mysql_fetch_assoc($result);
		if ($x % 2 == 0) {
			echo "<tr>";
		} else {
			echo "<tr bgcolor='#FBFBFB'>";
		}
		echo "<td align='center' valign='middle'><br /><form action='index.php' method='post'><input type='hidden' name='opportunityID' value='$row[opportunityID]'><input type='submit' value='View' class='submit' name='view' /></form></td>";
		echo "<td>$row[title]</td>";
		echo "<td>$row[numPositionsAvailable]</td>";
		echo "<td>$row[semester]</td>";
		echo "<td>$row[payRate]</td>";
		echo "<td>$row[duration]</td>";
		echo "<td></td>";
		echo "</tr>";
	}
	echo "</table>";
}

/**
 * @desc Filters the list by location, sorting by alphabetical order
 * @param int $city
 * @return null
 */
function filterLocation($city) {

	$result = mysql_query("SELECT DISTINCT city FROM organization");
	if (!$result
	)die("Could not complete the query...");
	$numRows = mysql_num_rows($result);
	echo "<form action='index.php' method='post'>";
	echo "<div align='left'>City: <select onchange='this.form.submit()' name='cityChange'></div>";
	if ($city == null) {
		echo "<option value=''>Select a city...</option>";
	}
	for ($i = 0; $i < $numRows; $i++) {
		$row = mysql_fetch_assoc($result);
		if (strcmp($row[city], $city) == 0) {
			echo "<option SELECTED value='$row[city]'>$row[city]</option>";
		} else {
			echo "<option value='$row[city]'>$row[city]</option>";
		}
	}
	echo "</select>";
	echo "</form>";
	$category = $_SESSION['category'];
	if ($city == null) {
		$query = "SELECT o.city, o.state, o.name, io.* FROM INTERNSHIP_OPPORTUNITY io, ORGANIZATION o WHERE io.organizationID = o.OrganizationID AND io.categories LIKE '%$category%' ORDER BY o.city";
	} else {
		$query = "SELECT o.city, o.state, o.name, io.* FROM INTERNSHIP_OPPORTUNITY io, ORGANIZATION o WHERE io.organizationID = o.OrganizationID AND o.city='$city' AND io.categories LIKE '%$category%' ORDER BY io.dateAdded DESC";
	}

	$result = mysql_query($query);
	if (!$result

	)die("Could not complete query...");
	$numRows = mysql_num_rows($result);
	echo "<table width='100%' cellspacing=0><tr><td width='10%'></td><td><b>Location</b></td><td><b>Position</b></td><td><b>Company</b></td><td width='10%'><b>Positions Available</b></td><td><b>Pay Rate</b></td><td><b>Semester</b></td><td width='30%'><b>Date Added</b></td></tr>";
	echo "<tr><td colspan=8><hr /></td></tr>";
	for ($x = 0; $x < $numRows; $x++) {
		$row = mysql_fetch_assoc($result);
		if ($x % 2 == 0) {
			echo "<tr>";
		} else {
			echo "<tr bgcolor='#FBFBFB'>";
		}
		echo "<td align='center' valign='middle'><br /><form action='index.php' method='post'><input type='hidden' name='viewCompany' value='$row[organizationID]'><input type='submit' value='View' class='submit' name='view' /></form></td>";
		echo "<td>$row[city], " . strtoupper($row[state]) . "</td>";
		echo "<td>$row[title]</td>";
		echo "<td>$row[name]</td>";
		echo "<td>$row[numPositionsAvailable]</td>";
		echo "<td>$row[payRate]</td>";
		echo "<td>$row[semester]</td>";
		$desc = $row[dateAdded];
		/* if (strlen($desc) > 50) {
		 $desc = substr($desc, 0, -(strlen($desc) - 50)) . "...";
		 } */
		echo "<td>$desc</td>";
		echo "</tr>";
	}
	echo "</table>";
}

/**
 * @desc Filters the list by company, sorts by alphabetical order
 * @param null
 * @return null
 */
function filterCompany() {
	$query = "SELECT * FROM ORGANIZATION ORDER BY name";
	$result = mysql_query($query);
	if (!$result

	)die("Could not complete query...");
	$numRows = mysql_num_rows($result);
	echo "<table width='100%' cellspacing=0><tr><td width='10%'></td><td><b>Name</b></td><td><b>Address</b></td><td><b>City</b></td><td><b>State</b></td><td><b>Zip</b></td><td width='30%'><b>Description</b></td></tr>";
	echo "<tr><td colspan=7><hr /></td></tr>";
	for ($x = 0; $x < $numRows; $x++) {
		$row = mysql_fetch_assoc($result);
		if ($x % 2 == 0) {
			echo "<tr>";
		} else {
			echo "<tr bgcolor='#FBFBFB'>";
		}
		echo "<td align='center' valign='middle'><br /><form action='index.php' method='post'><input type='hidden' name='viewCompany' value='$row[organizationID]'><input type='submit' value='View' class='submit' name='view' /></form></td>";
		echo "<td>$row[name]</td>";
		echo "<td>$row[address]</td>";
		echo "<td>$row[city]</td>";
		echo "<td>" . strtoupper($row[state]) . "</td>";
		echo "<td>$row[zip]</td>";
		$desc = $row[companyDescription];
		if (strlen($desc) > 50) {
			$desc = substr($desc, 0, -(strlen($desc) - 50)) . "...";
		}
		echo "<td>$desc</td>";
		echo "</tr>";
	}
	echo "</table>";
}

/**
 * @desc Addes slashes to a string, and gets rid of HTML entities
 * @param string $str
 * @return string $str
 */
function sanitizeStr($str) {
	$str = strip_tags($str);
	$str = addslashes($str);
	return $str;
}

/**
 * @desc Removes slashes from a string
 * @param string $str
 * @return string $str
 */
function restoreStr($str) {
	$str = stripslashes($str);
	return $str;
}



?>